<template>
  <div class="mainlogo">
    <div class="main-left"></div>
    <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
      <div class="title">管理员登录</div>
      <!-- <p class="jianjie">简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介</p> -->
      <a-form-model-item>
        <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed"></login-account>
      </a-form-model-item>
      

      <a-form-item style="margin-top:24px">
        <a-button size="large"  type="primary"  htmlType="submit"  class="login-button"  :loading="loginBtn"  @click.stop.prevent="handleSubmit" :disabled="loginBtn">确定
        </a-button>
      </a-form-item>
      <a-form-model-item>

        <a style="color: #1890ff;float: right;" @click="clickHelp">登录帮助</a>
      </a-form-model-item>
    </a-form-model>

    <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess" @cancel="stepCaptchaCancel"></two-step-captcha>
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <!--  <third-login ref="thirdLogin"></third-login> -->
    <login-help-modal ref="modalForm" @ok="modalFormOk"></login-help-modal>

  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'
import LoginHelpModal from './modules/LoginHelpModal'
import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'

export default {
    components: {
      LoginSelectTenant,
      TwoStepCaptcha,
      ThirdLogin,
      LoginAccount,
      LoginPhone,
      LoginHelpModal
    },
    data () {
      return {
        customActiveKey: 'tab1',
        rememberMe: true,
        loginBtn: false,
        requiredTwoStepCaptcha: false,
        stepCaptchaVisible: false,
        encryptedString:{
          key:"",
          iv:"",
        },
      }
    },
    created() {
      Vue.ls.remove(ACCESS_TOKEN)
      this.getRouterData();
      this.rememberMe = true
    },
    methods:{
      handleTabClick(key){
        this.customActiveKey = key
      },
      handleRememberMeChange(e){
        this.rememberMe = e.target.checked
      },
      /**跳转到登录页面的参数-账号获取*/
      getRouterData(){
        this.$nextTick(() => {
          let temp = this.$route.params.username || this.$route.query.username || ''
          if (temp) {
            this.$refs.alogin.acceptUsername(temp)
          }
        })
      },

      //登录
      handleSubmit () {
        this.loginBtn = true;
        if (this.customActiveKey === 'tab1') {
          // 使用账户密码登录
          this.$refs.alogin.handleLogin(this.rememberMe)
        } else {
          //手机号码登录
          this.$refs.plogin.handleLogin(this.rememberMe)
        }
        this.loginBtn = false;
      },
      // 校验失败
      validateFail(){
        this.loginBtn = false;
      },
      // 登录后台成功
      requestSuccess(loginResult){
        this.$refs.loginSelect.show(loginResult)
      },
      //登录后台失败
      requestFailed (err) {
        let description = ((err.response || {}).data || {}).message || err.message || "请求出现错误，请稍后再试"
        this.$notification[ 'error' ]({
          message: '登录失败',
          description: description,
          duration: 4,
        });
        //账户密码登录错误后更新验证码
        if(this.customActiveKey === 'tab1' && description.indexOf('密码错误')>0){
          this.$refs.alogin.handleChangeCheckCode()
        }
        this.loginBtn = false;
      },
      loginSelectOk(){
        let status = this.$store.getters.initStatus
        let userName = this.$store.getters.userInfo.username
        console.log('是否初始化',status)
        console.log('用户>>>',userName)
        if(!status&&userName!=='developer') {
         return this.$message.info('系统未初始化，请登陆开发人员账号配置') 
        }
        this.loginSuccess()
      },
      //登录成功
      loginSuccess () {
        // console.log(111111111)
        this.$router.push({ path: "/statistics/dataKanban" }).catch(()=>{
          console.log('登录跳转首页出错,这个错误从哪里来的')
        })
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`,
        });
      },

      stepCaptchaSuccess () {
        this.loginSuccess()
      },
      stepCaptchaCancel () {
        this.Logout().then(() => {
          this.loginBtn = false
          this.stepCaptchaVisible = false
        })
      },
      //获取密码加密规则
      getEncrypte(){
        var encryptedString = Vue.ls.get(ENCRYPTED_STRING);
        if(encryptedString == null){
          getEncryptedString().then((data) => {
            this.encryptedString = data
          });
        }else{
          this.encryptedString = encryptedString;
        }
      },
      clickHelp(){
        // this.$refs.modalForm.add();
        this.$refs.modalForm.get();
        this.$refs.modalForm.title = "登录帮助";
        this.$refs.modalForm.disableSubmit = false;
      },
      modalFormOk(){

      }
    }

  }
</script>
<style lang="less" scoped>
  .mainlogo {

        // min-width: 100%;
        width: 70%;
        height: 100%;
        margin: 0 auto;
        display: flex;
        // border: 1px solid red;
        .main-left{
         width: 50%;
         background: url(~@/assets/left.png) no-repeat;
         background-size: 100% 100%;
         margin-right: -10px;
        }
      }
  .user-layout-login {
    height: 100%;
    width: 50%;
    padding: 7% 8% 6% 9%;
    background: url(~@/assets/right.png) no-repeat 100%;
    margin-left: -10px;
    background-size: 100% 100%;
    .title{
      font-size: 18px;
      color:#333;
      width:100%;
      text-align:center;
      font-weight: 600;
      margin-bottom: 70px;
    }
    .jianjie{
      margin-top: 10px;
      font-size: 14px;
      color:rgba(0,0,0,.4);
      text-align:center;
      margin-bottom: 8%;
    }
    label {
      font-size: 14px;
    }
  .getCaptcha {
      display: block;
      width: 100%;
      height: 40px;
    }

  .forge-password {
      font-size: 14px;
    }

    button.login-button {
      padding: 0 15px;
      font-size: 16px;
      height: 40px;
      width: 100%;
    }

  .user-login-other {
      text-align: left;
      margin-top: 24px;
      line-height: 22px;

    .item-icon {
        font-size: 24px;
        color: rgba(0,0,0,.2);
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color .3s;

      &:hover {
          color: #1890ff;
        }
      }

    .register {
        float: right;
      }
    }
  }
  .ant-row{
    margin-bottom: 5%;
  }
</style>
<style>
  .valid-error .ant-select-selection__placeholder{
    color: #f5222d;
  }
  
</style>